<template>
  <view>
    <!--    <u-navbar title="通知公告" ></u-navbar>-->
    <uni-search-bar @confirm="search" v-model="searchValue">
    </uni-search-bar>
    <notice-view :notices="notices" @read="read"></notice-view>
    <uni-load-more v-if="queryParams.pageSize<notices.length" color="#007AFF" :status="status"/>
    <o-empty v-if="notices.length==0"></o-empty>
  </view>
</template>

<script>
import {NoticeService} from "./noticeService";
import NoticeView from "../../../components/NoticeView";
import OEmpty from "../../../../uni_modules/o-empty/components/o-empty/o-empty";

export default {
  components: {OEmpty, NoticeView},
  data() {
    return {
      searchValue: '',
      notices: [],
      queryParams: {
        currentPage: 1,
        pageSize: 10,
        filters: {
          userId: uni.getStorageSync('userInfo').eid
        },
        orderBys: [
          {
            property: "createDatetime",
            order: "desc"
          }
        ]
      },
      totalCount: 100,
      status: "more"
    }
  },
  created() {
    this.init(0);
  },
  onReachBottom() {
    console.log("上拉刷新")
    // uni.$emit('onReachBottom',"刷新数据");
    //触底业务逻辑
    if (this.queryParams.pageSize * this.queryParams.currentPage >= this.totalCount) {
      this.status = "nomore"
      return;
    }
    this.queryParams.currentPage++;
    this.init(1);
  },

  methods: {
    init(type) {
      this.findNoticesNotContent(type)
    },
    findNoticesNotContent(type) {
      this.status = "loading"
      if (type == 0) {
        this.notices = [];
        this.queryParams.currentPage = 1
      }
      NoticeService.findNoticesNotContent(this.queryParams).then(res => {
        //拼接数组
        this.notices = this.notices.concat(res.data.datas);
        this.totalCount = res.data.totalCount;
        this.status = 'more'
      })
    },
    read(index){
      this.notices[index].readStatus=0;
    },
    search(res) {
      if (res.value) {
        this.queryParams.filters.title = "*" + res.value + "*";
      } else {
        this.queryParams.filters.title = null;
      }
      this.queryParams.currentPage = 1;
      this.findNoticesNotContent(0);
    },
  },
  onUnload() {
    // plus.key.hideSoftKeybord();
    // console.log('back')
    let pages = getCurrentPages(); // 当前页面
    let beforePage = pages[pages.length - 2]; // 上一页
    let user=uni.getStorageSync('userInfo').userInfo
    let ref=user.userType=='user'?"userIndex":"repairIndex";
    console.log("ref",ref);
    beforePage.$vm.$refs[ref].refreshNotice = true;
    uni.navigateBack({
      delta:1,//返回层数，2则上上页
    })
  }
}
</script>

<style>

</style>
